package com.ebelter.seneo.utils;

import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.views.dialogs.picktime.IGenWheelView;
import com.ebelter.seneo.R;

/**
 * 身体成分判断标准
 */

public class BodyCompositionCriteria {




    //身体质量判断标准
    // 0偏瘦 1标准 2偏胖 3肥胖
    public static int bodyQualityStandard1(float BMI){
        //中国评价标准
        if (BMI<18.5){
            return 0;
        }else if (BMI>=18.5&&BMI<24){
            return 1;
        }else if (BMI>=24&&BMI<28){
            return 2;
        }else{
            return 3;
        }
    }


    public static float getWeightKgByBMI(float bmi,int heightCm){
        float heighM= (float) (heightCm*1.0/100.0);
        return NumUtils.numBaoLiuWei1(bmi*heighM*heighM);
    }





    public static int bodyQualityStandard2(float BMI){
        //WHO评价标准
        if (BMI<18.5){
            return 0;
        }else if (BMI>=18.5&&BMI<25){
            return 1;
        }else if (BMI>=25&&BMI<30){
            return 2;
        }else{
            return 3;
        }
    }



    public static float[] getTofatWeight(int age,int sex,int height){
        int[] t1=getFatPercentCriticalValue(age,sex);
        float w1 = getWeightKgByBMI(18.5f,height);
        float w2 = getWeightKgByBMI(24f,height);
        float w3 = getWeightKgByBMI(28f,height);

        float minFat= t1[0];

        float r1= (float) (w1*(100 - minFat)*1.0/100.0);
        float r2= (float) (w2*(100 - minFat)*1.0/100.0);
        float r3= (float) (w3*(100 - minFat)*1.0/100.0);

        float[] res=new float[3];
        res[0]=NumUtils.numBaoLiuWei1(r1);
        res[1]=NumUtils.numBaoLiuWei1(r2);
        res[2]=NumUtils.numBaoLiuWei1(r3);

        return res;
    }

    //0 偏低 1标准 2偏高 3超高
    public static int getTofatWeightLevel(int age,int sex,int height,float tofatWeight){

        float tofatweight=NumUtils.numBaoLiuWei1(tofatWeight);

        float[] res = getTofatWeight(age,sex,height);

        if (tofatWeight<res[0]){
            return 0;
        }else if (tofatWeight>=res[0]&&tofatweight<=res[1]){
            return 1;
        }else if (tofatWeight>res[1]&&tofatweight<=res[2]){
            return 2;
        }else {
            return 3;
        }
    }


    //根据年龄和性别获取脂肪率的临界值
    public static int[] getFatPercentCriticalValue(int age,int sex){
        int[] result = new int[3];
        if (sex==0){//男
            if (age<=39){
                result[0]=10;
                result[1]=21;
                result[2]=26;
            }else if (age>39&&age<=59){
                result[0]=11;
                result[1]=22;
                result[2]=26;
            }else {
                result[0]=13;
                result[1]=24;
                result[2]=29;
            }
        }else {//女
            if (age<=39){
                result[0]=20;
                result[1]=34;
                result[2]=39;
            }else if (age>39&&age<=59){
                result[0]=21;
                result[1]=35;
                result[2]=40;
            }else {
                result[0]=22;
                result[1]=36;
                result[2]=41;
            }
        }
        return result;
    }

    /**
     * 脂肪率标准
     * @param fatPercent 脂肪
     * @param age 年龄
     * @param sex 性别 0男1女
     * @return 0偏瘦 1标准- 2标准+ 3轻度肥胖 4重度肥胖
     */
    public static int getFatPercentStand(float fatPercent,int age,int sex) {

        if (fatPercent==0){
            return -1;
        }

        if (sex==0){//男
            if (age<=39){
                if (fatPercent >= 26) {   //高
                    return 4;
                } else if (fatPercent >= 21 && fatPercent < 26) {  //偏高
                    return 3;
                } else if (fatPercent >= 16 && fatPercent < 21) {   //标准
                    return 2;
                } else if (fatPercent >= 10 && fatPercent < 16) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }
            }else if (age>39&&age<=59){
                if (fatPercent >= 27) {   //高
                    return 4;
                } else if (fatPercent >= 22 && fatPercent < 27) {  //偏高
                    return 3;
                } else if (fatPercent >= 17 && fatPercent < 22) {   //标准
                    return 2;
                } else if (fatPercent >= 11 && fatPercent < 17) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }

            }else {
                if (fatPercent >= 29) {   //高
                    return 4;
                } else if (fatPercent >= 24 && fatPercent < 29) {  //偏高
                    return 3;
                } else if (fatPercent >= 19 && fatPercent < 24) {   //标准
                    return 2;
                } else if (fatPercent >= 13 && fatPercent < 19) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }
            }


        }else {//女
            if (age<=39){
                if (fatPercent >= 39) {   //高
                    return 4;
                } else if (fatPercent >= 34 && fatPercent < 39) {  //偏高
                    return 3;
                } else if (fatPercent >= 27 && fatPercent < 34) {   //标准
                    return 2;
                } else if (fatPercent >= 20 && fatPercent < 27) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }
            }else if (age>39&&age<=59){
                if (fatPercent >= 40) {   //高
                    return 4;
                } else if (fatPercent >= 35 && fatPercent < 40) {  //偏高
                    return 3;
                } else if (fatPercent >= 28 && fatPercent < 35) {   //标准
                    return 2;
                } else if (fatPercent >= 21 && fatPercent < 28) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }

            }else {
                if (fatPercent >= 41) {   //高
                    return 4;
                } else if (fatPercent >= 36 && fatPercent < 41) {  //偏高
                    return 3;
                } else if (fatPercent >= 29 && fatPercent < 36) {   //标准
                    return 2;
                } else if (fatPercent >= 22 && fatPercent < 29) {   //标准
                    return 1;
                } else {   //低
                    return 0;
                }
            }
        }

    }


    /**
     * 肌肉
     * @param muscleKg
     * @param heightCm
     * @param sex 0男1女
     * @return 0偏低 1正常 2偏高
     */
    public static int getMuscleStand(float muscleKg,int heightCm,int sex){
        float min;
        float max;
        if (sex==0){//男
            if (heightCm>=170){
                min=49.4f;
                max=59.4f;
            }else if (heightCm>=160&&heightCm<170){
                min=44f;
                max=52.4f;
            }else {
                min=38.5f;
                max=46.5f;
            }
        }else {//女
            if (heightCm>=160){
                min=36.5f;
                max=42.5f;
            }else if (heightCm>=150&&heightCm<160){
                min=32.9f;
                max=37.5f;
            }else {
                min=21.9f;
                max=34.7f;
            }
        }

        if (muscleKg>=max){
            return 2;
        }else if (muscleKg>=min&&muscleKg<=max){
            return 1;
        }else{
            return 0;
        }

    }


    /**
     *骨量
     * @param bondKg
     * @param weightKg
     * @param sex
     * @return  0偏低 1正常 2偏高
     */
    public static int getBoneStand(float bondKg,float weightKg,int sex){
        float value;
        if (sex==0){
            if (weightKg>=75){
                value=3.2f;
            }else if (weightKg>=60&&weightKg<75){
                value=2.9f;
            }else {
                value=2.5f;
            }
        }else {
            if (weightKg>=60){
                value=2.5f;
            }else if (weightKg>=45&&weightKg<60){
                value=2.2f;
            }else {
                value=1.8f;
            }
        }
        float min = value*0.85f;
        float max = value*1.15f;
        if (bondKg<min){
            return 0;
        }else if (bondKg>=min&&bondKg<=max){
            return 1;
        }else {
            return 2;
        }
    }


    /**
     *基础代谢率
     * @param Bmr
     * @param age
     * @param sex 0男1女
     * @return   0偏低 1正常 2偏高
     */
    public static int getBMRStand(int Bmr,int age,int sex) {

        if (Bmr==0){
            return -1;
        }

        int value;
        if (sex==0){
            if (age>69){
                value=1220;
            }else if (age>=50&&age<=69){
                value=1350;
            }else if (age>=30&&age<=49){
                value=1500;
            }else {
                value=1550;
            }

        }else {
            if (age>69){
                value=1010;
            }else if (age>=50&&age<=69){
                value=1110;
            }else if (age>=30&&age<=49){
                value=1170;
            }else {
                value=1210;
            }
        }

        float min = value*0.9f;
        float max = value*1.1f;

        if (Bmr<min){
            return 0;
        }else if (Bmr>=min&&Bmr<max){
            return 1;
        }else {
            return 2;
        }

    }



    //水分
    public static int getWaterStand(float waterPercent,int sex) {
        if (waterPercent==0){
            return -1;
        }
        float min,max;
        if (sex==0){
            min=50f;
            max=60f;
        }else {
            min=40f;
            max=60f;
        }
        if (waterPercent<min){
            return 0;
        }else if (waterPercent>=min&&waterPercent<=max){
            return 1;
        }else {
            return 2;
        }
    }


    /**
     *
     * @param visFat
     * @return 0健康 1警戒 2偏高 3危险
     */
    public static int getVisFatStand(float visFat) {
        if (visFat==0){
            return -1;
        }
        if (visFat>=15){
            return 3;
        }else if (visFat>=10&&visFat<15){
            return 2;
        }else if (visFat>=5&&visFat<10){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     *
     * @param proteinPercent
     * @return 0偏低 1正常 2过剩
     */
    public static int getProteinStand(float proteinPercent) {
        if (proteinPercent==0){
            return -1;
        }
        if (proteinPercent<16){
            return 0;
        }else if (proteinPercent>=16&&proteinPercent<=20){
            return 1;
        }else {
            return 2;
        }

    }

    /**
     *
     * @param weightKg
     * @return 0偏低 1正常 2稍胖 3很胖
     */
    public static int getWeightStand(float weightKg,int heightCm) {

        if (heightCm>0){
            float heightSq = heightCm * heightCm * 0.0001f;
            float max = heightSq * 28.0f;
            float nomal = heightSq * 24.0f;
            float min = heightSq * 18.5f;

            if (weightKg>max){
                return 3;
            }else if (weightKg>=nomal&&weightKg<max){
                return 2;
            }else if (weightKg>=min&&weightKg<nomal){
                return 1;
            }else if (weightKg<min){
                return 0;
            }
        }
        return 0;
    }


    //objects[0]=-1;
    //objects[1]= StringUtils.getResStr(R.string.pshx);
    public static Object[] getSize(float fatPercent,int age,int sex,float muscleKg,int heightCm){
        Object[] objects=new Object[2];

        if (fatPercent==0&&muscleKg==0){
            objects[0]=-1;
            objects[1]="";
            return objects;
        }

        int fatLevel = getFatPercentStand(fatPercent,age,sex);
        int muscleLevel = getMuscleStand(muscleKg,heightCm,sex);

        if (fatLevel==0){
            if (muscleLevel==0){
                objects[0]=6;
                objects[1]= StringUtils.getResStr(R.string.pshx);

            }else if (muscleLevel==1){
                objects[0]=7;
                objects[1]=StringUtils.getResStr(R.string.pshjrx);
            }else{
                objects[0]=8;
                objects[1]=StringUtils.getResStr(R.string.jrfdx);
            }

        }else if (fatLevel==1||fatLevel==2){

            if (muscleLevel==0){
                objects[0]=3;
                objects[1]=StringUtils.getResStr(R.string.ydbzx);

            }else if (muscleLevel==1){
                objects[0]=4;
                objects[1]=StringUtils.getResStr(R.string.biaozhun);
            }else{
                objects[0]=5;
                objects[1]=StringUtils.getResStr(R.string.jrx);
            }

        }else if (fatLevel==3||fatLevel==4){

            if (muscleLevel==0){
                objects[0]=0;
                objects[1]=StringUtils.getResStr(R.string.yxfpx);

            }else if (muscleLevel==1){
                objects[0]=1;
                objects[1]=StringUtils.getResStr(R.string.fpx);
            }else{
                objects[0]=2;
                objects[1]=StringUtils.getResStr(R.string.ppx);
            }

        }

        return objects;
    }



    }
